﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CoffeeBar.Models;
using CoffeeBar.Models.Webservice_Access;

namespace CoffeeBar.Controllers
{
    public class QuanlyhanghoaController : Controller
    {
        
        private DataAccess da=new DataAccess();
        //
        // GET: /Quanlyhanghoa/        
        public ActionResult Index()
        {
            return View();
        }
        [HttpGet]
        public ActionResult ThemLoaiHang()
        {
           // LoaiMatHangModels model = new LoaiMatHangModels();
            var temp = da.laydanhsach_LOAIHANG2(-11);
            
            var query = temp.Select(c => new { c.MALOAIHANG, c.TENLOAIHANG });
//             LoaiMatHangModels model=new LoaiMatHangModels();
//             model.LoaiMatHangItems= new SelectList(query.AsEnumerable(), "MALOAIHANG", "TENLOAIHANG");
            LoaiMatHang model=new LoaiMatHang();
            model.items_LoaiHang = new List<LoaiMatHang.LoaiHang>();

            foreach (var item in temp)
            {
                LoaiMatHang.LoaiHang a = new LoaiMatHang.LoaiHang();
                a.maloaihang = item.MALOAIHANG.ToString();
                a.tenloaihang = item.TENLOAIHANG;
                model.items_LoaiHang.Add(a);
            }
            ViewData["dsloaihang"] = model.items_LoaiHang;
            return View();
        }
          [HttpPost]
        public ActionResult ThemLoaiHang(LoaiMatHangModels model)
        {
            string _result = "Thêm thất bại";
            if (model.TenLoaiMatHang == "" || model.TenLoaiMatHang == null)
            {

            }
            else
            {
                da.them_LOAIHANG(model.TenLoaiMatHang);
                _result = "Đã thêm thành công" + model.TenLoaiMatHang;
            }
            ViewData["result"] = _result;
              return RedirectToAction("ThemLoaiHang",ViewData["result"]);
        }
          [HttpGet]
          public ActionResult DeleteLoaiHang(string id)
          {
              
              if(id.Count()>0)
              {
                  try
                  {
                      int temp = Convert.ToInt32(id);
                      da.xoa_LOAIHANG(temp);
                  }
                  catch (System.Exception ex)
                  {
                      return RedirectToAction("ThemLoaiHang");
                  }
              }
              return RedirectToAction("ThemLoaiHang");
          }
        [HttpGet]
          public ActionResult EditLoaiHang(string id, string item_name)
          {
              LoaiMatHang.LoaiHang model = new LoaiMatHang.LoaiHang();
              if (id != null && id != "" && item_name != null && item_name != "")
              {
                  model.maloaihang = Convert.ToInt32(id.ToString()).ToString();
                  model.tenloaihang = item_name.ToString();
              }
              ViewData["item"] = model;
              return View(ViewData["item"]);
          }
        [HttpPost]
        public ActionResult EditLoaiHang(string id,string id2, string tenloaihang)
        {
            bool isSuccess=false;
            
            isSuccess=da.sua_LOAIHANG(id,tenloaihang);
            if (isSuccess == false)
            {
                ViewData["result"] = "Cập nhật thất bại";
            }
            else
            {
                ViewData["result"] = "Cập nhật thành công!";
            }
            return View(ViewData["result"]);
        }
        public List<LoaiMatHang.LoaiHang> LayFull_DanhSachLoaiHang()
        {
            var dsloaihang = da.laydanhsach_LOAIHANG2(-11);
            List<LoaiMatHang.LoaiHang> loaihang = new List<LoaiMatHang.LoaiHang>();
            foreach (var item in dsloaihang)
            {
                LoaiMatHang.LoaiHang t = new LoaiMatHang.LoaiHang();
                t.maloaihang = item.MALOAIHANG.ToString();
                t.tenloaihang = item.TENLOAIHANG;
                loaihang.Add(t);
            }
            return loaihang;
        }
        [HttpGet]
        public ActionResult XemHangBan()
        {
            List<HangBan> model = new List<HangBan>();
            var items = da.laydanhsach_HANGBAN(-11);
            foreach (var item in items)
            {
                HangBan temp = new HangBan();
                temp.MaHang =(int) item.MAHANG;
                temp.MaLoaihang = (int)item.MALOAIHANG;
                temp.TenHang = item.TENHANG;
                temp.DonGia =(long) item.DONGIA;
                temp.GioiThieu = item.GIOITHIEU;
                temp.SanCo =(int) item.COSAN;
                temp.DiemDanhGia = (int)item.DIEMDANHGIA;
                model.Add(temp);
            }
            ViewData["dsHangBan"] = model;
           // MatHangModels model2 = new MatHangModels();
            var dsloaihang = da.laydanhsach_LOAIHANG2(-11);
            List<LoaiMatHang.LoaiHang> loaihang=new List<LoaiMatHang.LoaiHang>();
//             foreach (var item in dsloaihang)
//             {
//                 LoaiMatHang.LoaiHang t=new LoaiMatHang.LoaiHang();
//                 t.maloaihang=item.MALOAIHANG.ToString();
//                 t.tenloaihang=item.TENLOAIHANG;
//                 loaihang.Add(t);
//             }
            loaihang = LayFull_DanhSachLoaiHang();
            ViewData["dsLoaiHang"] = loaihang; 
            return View();
        }
        [HttpGet]
        public ActionResult DeleteHangBan(string id)
        {

            if (id.Count() > 0)
            {
                try
                {
                    int temp = Convert.ToInt32(id);
                    da.xoa_HANGBAN(temp);
                }
                catch (System.Exception ex)
                {
                    return RedirectToAction("XemHangBan");
                }
            }
            return RedirectToAction("XemHangBan");
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult ThemHangBan(MatHangModels model, string loaihangban,string txtgioithieu)
        {
            int cosan = -11;
            if (model.CoSan == true) cosan = 11;
            try
            {
                int temp = Convert.ToInt32(loaihangban);
                if (temp > 0) model.MaLoaiHang = temp;
                else
                {
                    ViewData["error_message"] = "Nhập sai -> Thao tác thêm thất bại!";
                    return View();
                }
            }
            catch (System.Exception ex)
            {
                ViewData["error_message"] = "Nhập sai -> Thao tác thêm thất bại!";
                return View();
            }
            try
            {                
                da.them_HANGBAN(model.MaLoaiHang, model.TenHang, model.DonGia, txtgioithieu, cosan, model.DiemDanhGia);
                ViewData["error_message"] = "Thêm vào thành công!!!";
            }
            catch (System.Exception ex)
            {
            	ViewData["error_message"]="Thêm vào thất bại!!!";
            }
           return  View();
        }
        [HttpGet]
        public ActionResult DetailHangBan(string id)
        {
//             try
//             {
//                 int k = Convert.ToInt32(id);
//                 if (k > 0)
//                 {
//                     var items = da.laydanhsach_HANGBAN(k);
//                     HangBan model = new HangBan();
//                     foreach (var item in items)
//                     {
//                         model.MaHang =(int) item.MAHANG;
//                         model.MaLoaihang =(int) item.MALOAIHANG;
//                         model.TenHang = item.TENHANG;
//                         model.DonGia = (long)item.DONGIA;
//                         model.GioiThieu = item.GIOITHIEU;
//                         model.SanCo =(int) item.COSAN;
//                         model.DiemDanhGia =(int) item.DIEMDANHGIA;
//                         break;
//                     }
//                     ViewData["model"] = model;
//                     return View();
//                 }
//                 else
//                 {
// 
//                     return RedirectToAction("XemHangBan");
//                 }
//             }
//             catch (System.Exception ex)
//             {
//                 return RedirectToAction("XemHangBan");
//             }      
            HangBan model = new HangBan();
            model = LayHangBan(id);
            ViewData["model"] = model;
            return View();
        }
        public HangBan LayHangBan(string id)
        {
            HangBan model = new HangBan();
            try
            {
                int k = Convert.ToInt32(id);
                if (k > 0)
                {
                    var items = da.laydanhsach_HANGBAN(k);
                   
                    foreach (var item in items)
                    {
                        model.MaHang = (int)item.MAHANG;
                        model.MaLoaihang = (int)item.MALOAIHANG;
                        model.TenHang = item.TENHANG;
                        model.DonGia = (long)item.DONGIA;
                        model.GioiThieu = item.GIOITHIEU;
                        model.SanCo = (int)item.COSAN;
                        model.DiemDanhGia = (int)item.DIEMDANHGIA;
                        break;
                    }
                    ViewData["model"] = model;                    
                }
                else
                {
                    return null;
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }
            return model;
        }
        [HttpGet]
        public ActionResult EditHangBan(string id)
        {
            List<LoaiMatHang.LoaiHang> loaihang = new List<LoaiMatHang.LoaiHang>();
            loaihang = LayFull_DanhSachLoaiHang();
            ViewData["dsLoaiHang"] = loaihang; 

            HangBan model = new HangBan();
            model = LayHangBan(id);
            ViewData["model"] = model;
            return View();
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult EditHangBan(string mahang, string tenhang, string loaihangban, string dongia, string txtgioithieu, string sanco, int diemdanhgia)
        {            
            try
            {
                da.sua_HANGBAN(Convert.ToInt32(mahang), Convert.ToInt32(loaihangban), tenhang, Convert.ToInt32(dongia), txtgioithieu, Convert.ToInt32(sanco), diemdanhgia);
                ViewData["result"] = "Cập Nhật Thành Công";
            }
            catch (System.Exception ex)
            {
                ViewData["result"] = "LỖI !Cập Nhật Thất Bại";
            }
            return View();
        }
    }
}
